from django.db import models

class Wallet(models.Model):
    user = models.OneToOneField('accounts.User', on_delete=models.CASCADE, related_name='wallet')
    balance = models.DecimalField(max_digits=10, decimal_places=2, default=0.00, verbose_name='账户余额')
    frozen_amount = models.DecimalField(max_digits=10, decimal_places=2, default=0.00, verbose_name='冻结金额')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'wallet'
        verbose_name = '用户钱包'
        verbose_name_plural = verbose_name
    
    def __str__(self):
        return f"{self.user.username}的钱包"


class WithdrawRequest(models.Model):
    STATUS_CHOICES = (
        ('pending', '待审核'),
        ('approved', '已批准'),
        ('rejected', '已拒绝'),
        ('completed', '已完成'),
    )
    
    wallet = models.ForeignKey(Wallet, on_delete=models.CASCADE, related_name='withdraw_requests')
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='提现金额')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending', verbose_name='状态')
    
    # 提现信息
    bank_name = models.CharField(max_length=100, verbose_name='银行名称')
    bank_account = models.CharField(max_length=50, verbose_name='银行账号')
    account_holder = models.CharField(max_length=50, verbose_name='账户持有人')
    
    # 审核信息
    reviewed_by = models.ForeignKey('accounts.User', on_delete=models.SET_NULL, null=True, blank=True, related_name='approved_withdrawals')
    reviewed_at = models.DateTimeField(null=True, blank=True, verbose_name='审核时间')
    review_notes = models.TextField(blank=True, verbose_name='审核备注')
    
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'withdraw_request'
        verbose_name = '提现申请'
        verbose_name_plural = verbose_name
    
    def __str__(self):
        return f"提现申请#{self.id} - {self.wallet.user.username}"